#include <errno.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/** Typedef for pointer to function being integrated. */
typedef double (*FnP)(double);

/** Dynamically load module modName and return a pointer to function
 *  modName() defined in that module.
 */
static FnP 
getFn(const char *modName)
{
  void *handle = dlopen(modName, RTLD_NOW);
  if (handle == NULL) {
    fprintf(stderr, "cannot open module '%s': %s\n", modName, dlerror());
    exit(1);
  }
  void *sym = dlsym(handle, modName);
  if (sym == NULL) {
    fprintf(stderr, "cannot link %s: %s\n", modName, dlerror()); exit(1);
  }
  return sym;
}

int 
main(int argc, const char *argv[])
{	
  if (argc != 3) {
    fprintf(stderr, "usage: %s FN_MODULE ARG\n", argv[0]); exit(1);
  }
  const char *modName = argv[1];
  double arg = strtod(argv[2], (char **)0); //no error checking
  FnP fn = getFn(modName);
  printf("%g\n", fn(arg));
  return 0;
}
